/**
 * Copyright (C) 2018-2020
 * All rights reserved, Designed By www.gzchengguo.cn

 */
package link.chengguo.orangemall.modules.wechat.rest;

import link.chengguo.orangemall.dozer.service.IGenerator;

import link.chengguo.orangemall.modules.aop.ForbidSubmit;
import link.chengguo.orangemall.modules.wechat.domain.OrangeWechatTemplate;
import link.chengguo.orangemall.modules.wechat.service.OrangeWechatTemplateService;
import link.chengguo.orangemall.modules.wechat.service.dto.OrangeWechatTemplateDto;
import link.chengguo.orangemall.modules.wechat.service.dto.OrangeWechatTemplateQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* @author chengguo
* @date 2019-12-10
*/
@Api(tags = "商城:微信模板管理")
@RestController
@RequestMapping("/api/orangeWechatTemplate")
@AllArgsConstructor
public class WechatTemplateController {


    private final OrangeWechatTemplateService orangeWechatTemplateService;
    private final IGenerator generator;


    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('admin','OrangeWechatTemplate:list')")
    public void download(HttpServletResponse response, OrangeWechatTemplateQueryCriteria criteria) throws IOException {
        orangeWechatTemplateService.download(generator.convert(orangeWechatTemplateService.queryAll(criteria), OrangeWechatTemplateDto.class), response);
    }

    @GetMapping
    @ApiOperation("查询微信模板消息")
    @PreAuthorize("@el.check('admin','OrangeWechatTemplate:list')")
    public ResponseEntity<Object> getOrangeWechatTemplates(OrangeWechatTemplateQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(orangeWechatTemplateService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @ForbidSubmit
    @PostMapping
    @ApiOperation("新增微信模板消息")
    @PreAuthorize("@el.check('admin','OrangeWechatTemplate:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody OrangeWechatTemplate resources){
        return new ResponseEntity<>(orangeWechatTemplateService.save(resources),HttpStatus.CREATED);
    }

    @ForbidSubmit
    @PutMapping
    @ApiOperation("修改微信模板消息")
    @PreAuthorize("@el.check('admin','OrangeWechatTemplate:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody OrangeWechatTemplate resources){
        orangeWechatTemplateService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @ForbidSubmit
    @ApiOperation("删除微信模板消息")
    @PreAuthorize("@el.check('admin','OrangeWechatTemplate:del')")
    @DeleteMapping("/{id}")
    public ResponseEntity<Object> deleteAll(@PathVariable Integer id) {
        orangeWechatTemplateService.removeById(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
